package com.tkjt.pms.modules.datainitail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkDataInitialEntity;
import com.tkjt.pms.modules.common.mapper.TkDataInitialMapper;
import com.tkjt.pms.modules.datainitail.service.TkDataInitialService;
import com.tkjt.pms.modules.datainitail.thread.DataInitialThread;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validator;
import java.util.Calendar;
import java.util.List;

/**   
 * @Title: Controller  
 * @Description: tk_index_value
 * @author onlineGenerator
 * @date 2019-11-21 10:48:59
 * @version V1.0   
 *
 */
@Slf4j
@RestController
@RequestMapping("/api/pms/dataInitial")
public class DataInitialController {
	private static final Logger logger = LoggerFactory.getLogger(DataInitialController.class);

	@Autowired
	private TkDataInitialService tkDataInitialService;
	@Autowired
	TkDataInitialMapper tkDataInitialMapper;

	/**
	 * 新年数据初始化
	 * @return
	 */
	@RequestMapping("resertData")
	public Result<?> resertData(String weight,String target,String relax,String radio,String grade) {
		QueryWrapper<TkDataInitialEntity> initialQueryWrapper = new QueryWrapper<>();
		initialQueryWrapper.lambda().eq(TkDataInitialEntity::getCode, "valueInitial");
		TkDataInitialEntity dataInitialEntity = new TkDataInitialEntity();
		dataInitialEntity.setStatus("2");
		tkDataInitialMapper.update(dataInitialEntity,initialQueryWrapper);
		new Thread(new DataInitialThread(tkDataInitialService,weight,target,relax,radio,grade)).start();
		return Result.ok("正在初始化数据，请稍后刷新...");
	}

	/**
	 * 新用户数据初始化
	 * @return
	 */
	@RequestMapping("initialDataForNewUser")
	public Result<?> initialDataForNewUser(HttpServletRequest req) {
		QueryWrapper<TkDataInitialEntity> initialQueryWrapper = new QueryWrapper<>();
		initialQueryWrapper.lambda().eq(TkDataInitialEntity::getCode, "newUserData");
		TkDataInitialEntity dataInitialEntity = new TkDataInitialEntity();
		dataInitialEntity.setStatus("2");
		tkDataInitialMapper.update(dataInitialEntity,initialQueryWrapper);
		try{
			tkDataInitialService.initialDataForNewUser();
		}catch (Exception e){
			dataInitialEntity.setStatus("4");
			tkDataInitialMapper.update(dataInitialEntity,initialQueryWrapper);
		}
		return Result.ok("初始化成功");
	}
}
